// 定长内存池实现
#include "Common.h"

#ifdef _WIN32
	#include <windows.h>
#elif
	//
#endif

//// 直接去堆上按页申请空间
//inline static void* SystemAlloc(size_t kpage)
//{
//#ifdef _WIN32
//	void* ptr = VirtualAlloc(0, kpage << 13, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
//#elif
//	// 
//#endif
//	if (ptr == nullptr)
//		throw std::bad_alloc();
//	return ptr;
//}


template<class T>
class ObjectPool
{
public:
	T* New()
	{
		T* obj = nullptr;

		// 首先尝试从_freeList中取空间
		if (_freeList != nullptr)
		{
			obj = (T*)_freeList;
			_freeList = *((void**)_freeList);
		}

		if (_remainBytes < sizeof(T))// 剩余空间小于sizeof（T）
		{
			_remainBytes = 128 * 1024;
			//_memory = (char*)malloc(_remainBytes);
			_memory = (char*)SystemAlloc(_remainBytes >> 13);
		}
		obj = (T*)_memory;
		size_t objSize = sizeof(obj) >= sizeof(void*) ? sizeof(obj) : sizeof(void*);
		_memory += objSize;
		_remainBytes -= objSize;

		new(obj)T;//显式调用T构造函数初始化
		return obj;
	}

	void Delete(T* obj)
	{
		obj->~T();//显式析构

		*((void**)obj) = _freeList;
		_freeList = obj;
	}

private:
	char* _memory = nullptr; // 指向大块内存的指针
	size_t _remainBytes = 0; // 大块内存在切分过程中剩余字节数

	void* _freeList = nullptr; // 还回来过程中链接的自由链表的头指针
};

struct TreeNode
{
	int _val;
	TreeNode* _left;
	TreeNode* _right;

	TreeNode()
		:_val(0)
		, _left(nullptr)
		, _right(nullptr)
	{}
};

inline void TestObjectPool()
{
	// 申请释放的轮次
	const size_t Rounds = 5;

	// 每轮申请释放多少次
	const size_t N = 100000;

	std::vector<TreeNode*> v1;
	v1.reserve(N);

	size_t begin1 = clock();
	for (size_t j = 0; j < Rounds; ++j)
	{
		for (int i = 0; i < N; ++i)
		{
			v1.push_back(new TreeNode);
		}
		for (int i = 0; i < N; ++i)
		{
			delete v1[i];
		}
		v1.clear();
	}

	size_t end1 = clock();

	std::vector<TreeNode*> v2;
	v2.reserve(N);

	ObjectPool<TreeNode> TNPool;
	size_t begin2 = clock();
	for (size_t j = 0; j < Rounds; ++j)
	{
		for (int i = 0; i < N; ++i)
		{
			v2.push_back(TNPool.New());
		}
		for (int i = 0; i < N; ++i)
		{
			TNPool.Delete(v2[i]);
		}
		v2.clear();
	}
	size_t end2 = clock();

	cout << "new cost time:" << end1 - begin1 << endl;
	cout << "object pool cost time:" << end2 - begin2 << endl;
}